/*
ID: icerupt1
PROG: snail
LANG: C++11
*/

/* solution
 *
 * good.
 * 本来以为是很牛逼的题。。。结果深搜就可以过了。。。
 *
*/
#include <fstream>
#include <iostream>

std::ifstream fin {"snail.in" };
std::ofstream fout{"snail.out"};

int const maxn = 122;
bool barrier[maxn][maxn];
bool vis[maxn][maxn];
bool vis_b[maxn][maxn][4];
int n, m, ans = 0;

int const opt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

bool inrange(int x, int y)
{
	return (1 <= x && x <= n) && (1 <= y && y <= n);
}

void dfs(int x, int y, int deep, int dir)
{
	int px = x + opt[dir][0], py = y + opt[dir][1];
	if (vis[px][py]) {
		ans = std::max(ans, deep);
		return;
	} else
	if (!inrange(px, py)) {
		vis[px][py] = true;
		dfs(x, y, deep, (dir + 1) % 4);
		dfs(x, y, deep, (dir + 3) % 4);
		vis[px][py] = false;
	} else
	if (barrier[px][py]) {
		if (vis_b[px][py][dir]) {
			ans = std::max(ans, deep);
			return;
		}
		vis_b[px][py][dir] = true;
		dfs(x, y, deep, (dir + 1) % 4);
		dfs(x, y, deep, (dir + 3) % 4);
		vis_b[px][py][dir] = false;
	} else {
		int td = deep;
		while (inrange(px, py) && !barrier[px][py] && !vis[px][py]) {
			td++;
			vis[px][py] = true;
			px += opt[dir][0];  py += opt[dir][1];
		}
		px -= opt[dir][0];  py -= opt[dir][1];
		dfs(px, py, td, dir);
		while (!(px == x && py == y)) {
			vis[px][py] = false;
			px -= opt[dir][0];  py -= opt[dir][1];
		}
	}
}

int main()
{
	fin >> n >> m;
	for (int i = 0, x; i < m; i++) {
		char y;
		fin >> y >> x;
		barrier[x][y - 'A' + 1] = true;
	}
	vis[1][1] = true;
	dfs(1, 1, 1, 0);
	dfs(1, 1, 1, 1);

	std::cout << ans << '\n';
	fout << ans << '\n';
}

